﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ComponentFactory.Krypton.Toolkit;
using Bamboo.Core.Common;
namespace TinyERP.Framework.Controls
{
    public class DateTextBox: KryptonMaskedTextBox
    {
        private string dateFormat = "dd/MM/yyyy";
        public string DateFormat { get; set; }
        protected string CreateMaskFromDateFormat()
        {
            string replacedMaskFromDateFormat = dateFormat;
            replacedMaskFromDateFormat = replacedMaskFromDateFormat.Replace('d', '0');
            replacedMaskFromDateFormat = replacedMaskFromDateFormat.Replace('D', '0');
            replacedMaskFromDateFormat = replacedMaskFromDateFormat.Replace('M', '0');
            replacedMaskFromDateFormat = replacedMaskFromDateFormat.Replace('m', '0');
            replacedMaskFromDateFormat = replacedMaskFromDateFormat.Replace("yyyy", "0000");
            return replacedMaskFromDateFormat;
        }
        public DateTextBox()
            : base()
        {
            this.dateFormat = "dd/MM/yyyy";
            this.Mask = CreateMaskFromDateFormat();
            this.Text = "";
        }
        public DateTime Value
        {
            get
            {
                DateTime date = DateUtil.NULL_DATE;
                try
                {
                    date = DateUtil.GetDateFromString(this.Text, dateFormat);
                }
                catch
                {
                }
                return date;
            }
            set
            {
                string dateValue = DateUtil.GetStringFromDate(value, dateFormat);
                this.Text = dateValue;
            }
        }
    }
}
